<script setup>
import { ref } from "vue";
import { loginApi } from "@/api/login";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

const router = useRouter();
const loginForm = ref({
  username: "",
  password: "",
});

const login = async () => {
  const { code, data, msg } = await loginApi(loginForm.value);
  if (code) {
    ElMessage.success("登录成功");
    localStorage.setItem("loginUser", JSON.stringify(data));
    router.push("/");
  } else {
    ElMessage.error(msg);
  }
};
const clear = () => {
  loginForm.value.username = "";
  loginForm.value.password = "";
};
</script>

<template>
  <div id="container">
    <div class="login-form">
      <el-form label-width="80px">
        <p class="title">Tlias智能学习辅助系统</p>
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button class="button" type="primary" @click="login"
            >登 录</el-button
          >
          <el-button class="button" type="info" @click="clear">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="scss">
#container {
  background-image: url("@/assets/bg1.jpg");
  height: 100vh;
  padding-top: 10%;
  background-repeat: no-repeat;
  background-size: cover;
}
.login-form {
  width: 400px;
  margin: 0 auto;
  padding: 30px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  border-radius: 10px;
  background-color: white;
  border: 1px solid #e0e0e0;
  .title {
    margin-bottom: 30px;
    text-align: center;
    font-size: 30px;
    font-weight: bold;
    font-family: "楷体";
  }
  .button {
    width: 90px;
  }
}
</style>
